from typing import List, Optional, Sequence
from pydantic import BaseModel, Field, validator
from schemas import _validator


class ParticipantBase(BaseModel):
    accesstoken: Optional[str]
    vote_id: Optional[int]
    name: str
    vote_got: str
    start_time: str
    end_time: str
    img_path: Sequence[str]
    desc: str

    video_url: str


class ParticipantCreate(ParticipantBase):

    # 转json
    container_json_encode = validator(
        "img_path", allow_reuse=True)(_validator.container_json_encode)

    class Config:
        schema_extra = {
            "example": {
                  "accesstoken": "TOKEN3590",
                  "vote_id": 1,
                  "name": "投票项名称一",
                  "vote_got": 0,
                  "img_path": [
                    "http://slave.homed.me/1.jpg",
                    "http://slave.homed.me/2.jpg"
                  ],
                  "desc": "调查业主对物业的满意度情况，以便改进工作",
                  "start_time": "2022-03-10 08:00:00",
                  "end_time": "2022-03-15 08:30:00",
                  "video_url": "http://127.0.0.1/"
                }
        }


class ParticipantAdjust(ParticipantCreate):
    id: int

    class Config:
        schema_extra = {
            "example": {
                      "accesstoken": "TOKEN3590",
                      "id": 1,
                      "name": "投票项名称一",
                      "vote_got": 0,
                      "img_path": [
                        "http://slave.homed.me/1.jpg",
                        "http://slave.homed.me/2.jpg"
                      ],
                      "desc": "调查业主对物业的满意度情况，以便改进工作",
                      "start_time": "2022-03-10 08:00:00",
                      "end_time": "2022-03-15 08:30:00",
                      "video_url": "http://127.0.0.1/"
                    }
        }


class ParticipantDelete(BaseModel):
    id: Sequence[int]


class Participant(ParticipantBase):
    id: int

    # 转 dict
    container_json_decode = validator("img_path", pre=True, allow_reuse=True)(
        _validator.container_json_decode)
    # datetime 转换
    datetime_str_decode = validator(
        "start_time", "end_time", pre=True, allow_reuse=True)(
        _validator.datetime_str_decode)

    class Config:
        orm_mode = True


if __name__ == '__main__':
    from sql_app.database import SessionLocal
    from sql_app.models import TActivityVote

    db = SessionLocal()
    res = db.query(TActivityVote).filter(TActivityVote.id==1).scalar()
    res.name = "MERGE测试测试"
    db.merge(res)
    db.refresh(res)
    db.commit()
